package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wangshuai
 * @since 2021-02-04
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired   //远程调用
    private VodClient vodClient;

    @GetMapping("videoList/{chapterId}")
    public R videoList(@PathVariable String chapterId) {

        List<EduVideo> list = videoService.getVideoListById(chapterId);

        return R.ok().data("data",list);
    }

    //添加
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {

        videoService.save(eduVideo);

        return R.ok();
    }

    //查询
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id) {

        EduVideo eduVideo = videoService.getById(id);

        return R.ok().data("data",eduVideo);
    }

    //修改
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {

        videoService.updateById(eduVideo);

        return R.ok();
    }

    //删除
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {

        //先删视频，再删小节，因为要查videoId
        EduVideo eduVideo = videoService.getById(id);

        String videoId = eduVideo.getVideoSourceId();

        if (!StringUtils.isEmpty(videoId)) {
            vodClient.deleteVideoById(videoId);
        }

        videoService.removeById(id);

        return R.ok();
    }
}

